package com.iter.devbox.juc;

import java.util.concurrent.CountDownLatch;

public class Driver {

	public static void main(String[] args) throws InterruptedException {
		int N = 10;
		CountDownLatch startSignal = new CountDownLatch(1);
		CountDownLatch doneSignal = new CountDownLatch(N);

		for (int i = 0; i < N; ++i) {
			// create and start threads
			new Thread(new Worker(startSignal, doneSignal)).start();
		}
		System.out.println("don't let run yet......"); // don't let run yet
		startSignal.countDown(); // let all threads proceed
		System.out.println("let all threads proceed......");
		doneSignal.await(); // wait for all to finish
		System.out.println("all thread finish......");
	}
}

class Worker implements Runnable {
	
	private final CountDownLatch startSignal;
	
	private final CountDownLatch doneSignal;

	Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
		this.startSignal = startSignal;
		this.doneSignal = doneSignal;
	}

	public void run() {
		try {
			startSignal.await();
			doWork();
			doneSignal.countDown();
		} catch (InterruptedException ex) {
			ex.printStackTrace();
		}
	}

	void doWork() {
		System.out.println("doWork......");
	}
}